package 作业;

/**
 * Created with IntelliJ IDEA.
 * Description:将count和increase方法都定义为了类方法-->实例化一个静态类
 * User: lenovo
 * Date: 2022-09-24
 * Time: 22:44
 */
public class Demo1_1 {
    static class Counter{
        public int count;
        public synchronized void increase(){
            count++;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                counter.increase();
            }

        },"这是线程1");

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                counter.increase();
            }

        },"这是线程2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter.count);
    }
}
